package com.leon.datalink.web.testcontroller;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.leon.datalink.core.utils.JacksonUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.management.*;
import java.util.Random;

/**
 * GC metrics controller for development - simulates garbage collection metrics
 */
@RestController
@RequestMapping("/actuator/metrics")
@CrossOrigin(origins = "*", maxAge = 3600)
public class GCMetricsController {

    private final Random random = new Random();
    private final MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();

    @GetMapping("/jvm.gc.memory.allocated")
    public Object getJvmGcMemoryAllocated() {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("name", "jvm.gc.memory.allocated");
        result.put("description", "Incremented for an increase in the size of the young generation memory pool after one GC to before the next");
        result.put("baseUnit", "bytes");
        
        ArrayNode measurements = JacksonUtils.createEmptyArrayNode();
        ObjectNode measurement = JacksonUtils.createEmptyJsonNode();
        measurement.put("statistic", "COUNT");
        measurement.put("value", random.nextLong(100000000L, 1000000000L)); // 模拟GC分配内存
        measurements.add(measurement);
        
        result.set("measurements", measurements);
        return result;
    }

    @GetMapping("/jvm.gc.memory.promoted")
    public Object getJvmGcMemoryPromoted() {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("name", "jvm.gc.memory.promoted");
        result.put("description", "Count of positive increases in the size of the old generation memory pool before GC to after GC");
        result.put("baseUnit", "bytes");
        
        ArrayNode measurements = JacksonUtils.createEmptyArrayNode();
        ObjectNode measurement = JacksonUtils.createEmptyJsonNode();
        measurement.put("statistic", "COUNT");
        measurement.put("value", random.nextLong(10000000L, 100000000L)); // 模拟GC提升内存
        measurements.add(measurement);
        
        result.set("measurements", measurements);
        return result;
    }

    @GetMapping("/jvm.gc.max.data.size")
    public Object getJvmGcMaxDataSize() {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("name", "jvm.gc.max.data.size");
        result.put("description", "Max size of old generation memory pool");
        result.put("baseUnit", "bytes");
        
        ArrayNode measurements = JacksonUtils.createEmptyArrayNode();
        ObjectNode measurement = JacksonUtils.createEmptyJsonNode();
        measurement.put("statistic", "VALUE");
        measurement.put("value", memoryBean.getHeapMemoryUsage().getMax() * 0.7); // 模拟老年代最大大小
        measurements.add(measurement);
        
        result.set("measurements", measurements);
        return result;
    }

    @GetMapping("/jvm.gc.live.data.size")
    public Object getJvmGcLiveDataSize() {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("name", "jvm.gc.live.data.size");
        result.put("description", "Size of old generation memory pool after a full GC");
        result.put("baseUnit", "bytes");
        
        ArrayNode measurements = JacksonUtils.createEmptyArrayNode();
        ObjectNode measurement = JacksonUtils.createEmptyJsonNode();
        measurement.put("statistic", "VALUE");
        measurement.put("value", memoryBean.getHeapMemoryUsage().getUsed() * 0.6); // 模拟老年代当前大小
        measurements.add(measurement);
        
        result.set("measurements", measurements);
        return result;
    }

    @GetMapping("/jvm.gc.pause")
    public Object getJvmGcPause() {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("name", "jvm.gc.pause");
        result.put("description", "Time spent in GC pause");
        result.put("baseUnit", "seconds");
        
        ArrayNode measurements = JacksonUtils.createEmptyArrayNode();
        
        // 模拟不同的GC暂停统计
        ObjectNode countMeasurement = JacksonUtils.createEmptyJsonNode();
        countMeasurement.put("statistic", "COUNT");
        countMeasurement.put("value", random.nextInt(100, 1000));
        measurements.add(countMeasurement);
        
        ObjectNode totalTimeMeasurement = JacksonUtils.createEmptyJsonNode();
        totalTimeMeasurement.put("statistic", "TOTAL_TIME");
        totalTimeMeasurement.put("value", random.nextDouble() * 10); // 0-10秒总暂停时间
        measurements.add(totalTimeMeasurement);
        
        ObjectNode maxMeasurement = JacksonUtils.createEmptyJsonNode();
        maxMeasurement.put("statistic", "MAX");
        maxMeasurement.put("value", random.nextDouble() * 0.5); // 0-0.5秒最大暂停时间
        measurements.add(maxMeasurement);
        
        result.set("measurements", measurements);
        return result;
    }
}
